from datetime import time
import random

from django.db.models import *
from django.utils import timezone

from apps.gzde.models import Goods
from apps.system.models import User
from extensions.models import *

# 地区
class Personal_Center_Region(Model):
    # 名称
    name = CharField(max_length=64, verbose_name='名称')
    # 父级
    parent = ForeignKey('self', on_delete=SET_NULL, null=True, blank=True, verbose_name='父级',related_name='region_children', db_constraint=False)
    # 级别
    level = IntegerField(verbose_name='级别', null=True, blank=True)
    # 排序
    sort = IntegerField(verbose_name='排序', null=True, blank=True)
    # 编号
    code = CharField(max_length=64, verbose_name='编号', null=True, blank=True)

#收货地址
class Address(Model):
    """收货地址"""
    #收货人
    name = CharField(max_length=64, verbose_name='收货人')
    #收货人电话
    phone = CharField(max_length=64, verbose_name='收货人电话')
    #省
    province = ForeignKey(Personal_Center_Region, on_delete=SET_NULL, null=True, blank=True, verbose_name='省',related_name='Address_province', db_constraint=False)
    #市
    city = ForeignKey(Personal_Center_Region, on_delete=SET_NULL, null=True, blank=True, verbose_name='市',related_name='Address_city', db_constraint=False)
    #区县
    district = ForeignKey(Personal_Center_Region, on_delete=SET_NULL, null=True, blank=True, verbose_name='区县',related_name='Address_district', db_constraint=False)
    #地区
    region = CharField(max_length=64,  null=True, blank=True, verbose_name='地区')
    #详细地址
    detail = CharField(max_length=64, verbose_name='详细地址')
    #是否默认
    is_default = IntegerField(unique=False, default=0, blank=True, verbose_name='是否默认')
    #用户
    user = ForeignKey(User, on_delete=SET_NULL, null=True, blank=True, verbose_name='用户',related_name='Address_user',db_constraint=False)
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)
    update_time = DateTimeField(auto_now=True, verbose_name='更新时间', null=True, blank=True)
    del_flag = BooleanField(default=False, null=True, blank=True, verbose_name='删除标志')  # 0=未删除1=已删除

    class Meta:
        unique_together = (('name', 'phone','region','detail'),)

# 订单
class Order(Model):
    # 订单状态选项
    ORDER_STATUS_CHOICES = [
        ("cancel", "已取消"),
        ("yiyuyue", "已预约"),
        ("yijiedan", "已接单"),
        ("xianchangchakan", "已查勘"),
        ("yibaojia", "已报价"),
        ("yiqueren", "双方已确认"),
        ("xuqianhetong", "已签合同"),
        ("jiaofu", "已实施交付"),
        ("querenjiaofu", "已确认交付"),
        ("yishoukuan", "已收款"),
        ("daipingjia", "待评价"),
        ("yipingjia", "已评价"),
    ]
    # 订单号
    order_no = CharField(max_length=64, verbose_name='订单号')
    # 用户
    user = ForeignKey(User, on_delete=SET_NULL, null=True, blank=True, verbose_name='用户',related_name='Order_user',db_constraint=False)
    # 收货地址
    address = ForeignKey(Address, on_delete=SET_NULL, null=True, blank=True, verbose_name='收货地址',related_name='Order_address',db_constraint=False)
    # 订单状态
    order_status = CharField(max_length=64,default="yiyuyue",choices=ORDER_STATUS_CHOICES, verbose_name='订单状态')
    # 订单总金额
    order_amount = DecimalField(max_digits=10, null=True, default=0, blank=True,decimal_places=2, verbose_name='订单总金额')
    # 订单实付金额
    order_pay_amount = DecimalField(max_digits=10,null=True, default=0, blank=True, decimal_places=2, verbose_name='订单实付金额')
    # 订单运费金额
    order_freight_amount = DecimalField(max_digits=10,null=True, default=0, blank=True, decimal_places=2, verbose_name='订单运费金额')
    # 订单优惠金额
    order_discount_amount = DecimalField(max_digits=10, null=True, default=0, blank=True,decimal_places=2, verbose_name='订单优惠金额')
    # 订单支付方式
    order_pay_type = IntegerField(unique=False, default=0, blank=True, verbose_name='订单支付方式')
    # 订单支付时间
    order_pay_time = DateTimeField(auto_now_add=True, verbose_name='订单支付时间', null=True, blank=True)
    # 订单发货时间
    order_delivery_time = DateTimeField(auto_now_add=True, verbose_name='订单发货时间', null=True, blank=True)
    # 订单完成时间
    order_complete_time = DateTimeField(auto_now_add=True, verbose_name='订单完成时间', null=True, blank=True)
    # 订单关闭时间
    order_close_time = DateTimeField(auto_now_add=True, verbose_name='订单关闭时间', null=True, blank=True)
    # 订单备注
    order_remark = CharField(max_length=64, verbose_name='订单备注', null=True, blank=True)
    # 商品
    goods = ManyToManyField(Goods, verbose_name='商品', related_name='Order_goods', db_constraint=False)
    # 订单商品数量
    order_goods_num = IntegerField(unique=False, default=0, blank=True, verbose_name='订单商品数量')
    # 需求描述，用长文本
    demand_desc = TextField(verbose_name='需求描述', null=True, blank=True)
    # 创建时间
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)
    # 更新时间
    update_time = DateTimeField(auto_now=True, verbose_name='更新时间', null=True, blank=True)
    # 删除标志
    del_flag = BooleanField(default=False, null=True, blank=True, verbose_name='删除标志')  # 0=未删除1=已删除

    def __str__(self):
        return self.order_no

    #自动生成订唯一单号，时间戳+随机数
    def _generate_order_no(self):
        # 订单号前缀
        # prefix = 'SZGCDD'
        # 获取当前时间并格式化为年月日时分秒（YYYYMMDDHHMMSS）
        timestamp = timezone.now().strftime('%Y%m%d%H%M%S')
        # 生成一个5位的随机数
        random_digits = random.randint(10000, 99999)
        # 组合为订单号
        # return prefix + timestamp + str(random_digits)
        return timestamp + str(random_digits)


    def save(self, *args, **kwargs):
        if not self.order_no:
            # 只有在订单号不存在的情况下才生成新的订单号
            self.order_no = self._generate_order_no()
        super(Order, self).save(*args, **kwargs)


# 现场图片
class Order_ScenePhoto(Model):
    #订单
    order = ForeignKey(Order, on_delete=SET_NULL, null=True, blank=True, verbose_name='订单',related_name='Order_ScenePhoto',db_constraint=False)
    file = ImageField(upload_to='order_photos/')
    name = CharField(max_length=256, null=True, verbose_name='图片名称')
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)

    def __str__(self):
        return self.file.name

# 现场图片临时表
class Order_ScenePhoto_temp(Model):
    file = ImageField(upload_to='order_photos_temp/')
    name = CharField(max_length=256, null=True, verbose_name='图片名称')

    def __str__(self):
        return self.file.name

# 订单评价
class Order_Evaluate(Model):
    # 订单
    order = ForeignKey(Order, on_delete=SET_NULL, null=True, blank=True, verbose_name='订单',related_name='Order_Evaluate',db_constraint=False)
    # 评价内容
    evaluate_content = CharField(max_length=256, null=True, verbose_name='评价内容')
    # 评价等级,10=好评，5=中评，1=差评
    evaluate_level = IntegerField(unique=False, default=10, blank=True, verbose_name='评价等级')
    # 评价时间
    evaluate_time = DateTimeField(auto_now_add=True, verbose_name='评价时间', null=True, blank=True)
    # 创建时间
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)
    # 更新时间
    update_time = DateTimeField(auto_now=True, verbose_name='更新时间', null=True, blank=True)
    # 删除标志
    del_flag = BooleanField(default=False, null=True, blank=True, verbose_name='删除标志')  # 0=未删除1=已删除

    def __str__(self):
        return self.evaluate_content


# 订单评价图片
class Order_EvaluatePhoto(Model):
    # 订单评价
    order_evaluate = ForeignKey(Order_Evaluate, on_delete=SET_NULL, null=True, blank=True, verbose_name='订单评价',related_name='Order_EvaluatePhoto',db_constraint=False)
    file = ImageField(upload_to='order_evaluate_photos/')
    name = CharField(max_length=256, null=True, verbose_name='图片名称')
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)

    def __str__(self):
        return self.file.name


# 施工队伍
class Construction_Team(Model):
    #名称
    name = CharField(max_length=64, null=True, verbose_name='施工队伍名称')
    # 行业
    industry = CharField(max_length=64, null=True, verbose_name='行业')
    #详情介绍
    detail = TextField(verbose_name='详情介绍', null=True, blank=True)
    # 联系人
    contact = CharField(max_length=64, null=True, verbose_name='联系人')
    #职务
    position = CharField(max_length=64, null=True, verbose_name='职务')
    # 联系电话
    contact_phone = CharField(max_length=64, null=True, verbose_name='联系电话')
    # 联系地址
    contact_address = CharField(max_length=256, null=True, verbose_name='联系地址')
    # 身份证号
    id_card = CharField(max_length=64, null=True, verbose_name='身份证号')
    # 属性，1=企业，2=个人
    construction_team_type = IntegerField(unique=False, default=1, blank=True, verbose_name='属性')
    # 创建时间
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)
    # 更新时间
    update_time = DateTimeField(auto_now=True, verbose_name='更新时间', null=True, blank=True)
    # 删除标志
    del_flag = BooleanField(default=False, null=True, blank=True, verbose_name='删除标志')  # 0=未删除1=已删除
    # 备注
    remark = CharField(max_length=256, null=True, verbose_name='备注')
    #创建人
    create_user = ForeignKey(User, on_delete=SET_NULL, null=True, blank=True, verbose_name='创建人',related_name='Construction_Team_create_user',db_constraint=False)
    #更新人
    update_user = ForeignKey(User, on_delete=SET_NULL, null=True, blank=True, verbose_name='更新人',related_name='Construction_Team_update_user',db_constraint=False)

    def __str__(self):
        return self.name

# 施工队伍图片
class Construction_TeamPhoto(Model):
    #施工队伍
    construction_team = ForeignKey(Construction_Team, on_delete=SET_NULL, null=True, blank=True, verbose_name='施工队伍',related_name='Construction_TeamPhoto',db_constraint=False)
    file = ImageField(upload_to='construction_team_photos/')
    name = CharField(max_length=256, null=True, verbose_name='图片名称')
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)

    def __str__(self):
        return self.file.name



